home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / emulation / amigavgb_ppc / demos / space.s < prev    next >
Text File  |  1999-05-17  |  36KB  |  1,294 lines

  1.     ; Little demo illustrating how to use the graphical possibilities
  2.     ; of the GB (background, window and animated sprite).
  3.     ; I have used fixed-point values for both the position and
  4.     ; speed of objects to get smooth movements.
  5.     ;
  6.     ; OBJ data        : 0x8000 -> 0x8FFF (unsigned).
  7.     ; Window data        : 0x8800 -> 0x97FF (unsigned).
  8.     ; Background data    : 0x8800 -> 0x97FF (signed).
  9.     ;
  10.     ; Tiled 0xFC -> 0xFF are standard tiles (all black -> all white).
  11.     ;
  12.     ; Keys:
  13.     ; Directions        : Change the speed (and direction) of the sprite
  14.     ; Directions + A    : Change the speed (and direction) of the window
  15.     ; Directions + B    : Change the speed (and direction) of the background
  16.     ; START            : Open/close the door
  17.     ; SELECT        : Basic fading effect
  18.     ;
  19.     ; Note that the window is kept in the lower right part of the screen
  20.     ; since it can't be made transparent.
  21.  
  22.     .include    "lib/global.h"
  23.  
  24.     .DELAY        =    0x1000            ; Delay value to slow the animation
  25.     .NBDFRAMES    =    .endfilm-.film        ; Nb frames for the door
  26.     .NBSFRAMES    =    0x07            ; Nb frames for the sprite
  27.     .WINSZX        =    0x80            ; Size of the picture in the window
  28.     .WINSZY        =    0x50
  29.     .MINWINX    =    .MAXWNDPOSX-.WINSZX+1    ; Bounds of the window origin
  30.     .MINWINY    =    .MAXWNDPOSY-.WINSZY+1
  31.     .MAXWINX    =    .MAXWNDPOSX
  32.     .MAXWINY    =    .MAXWNDPOSY
  33.     .FADESTEP    =    0x10            ; Nb steps for the fading effect
  34.     .STARTFADE    =    0x06*.FADESTEP        ; Initial value for the fading effect
  35.  
  36.     .module    Demo
  37.  
  38.     .area    _BSS
  39.  
  40. .time:                ; Global "time" value (counter)
  41.     .ds    0x01
  42.  
  43. .doorstate:            ; State of the door (OPENED, CLOSED...)
  44.     .ds    0x01
  45. .doorpos:            ; Current position in the door animation
  46.     .ds    0x01
  47.  
  48. .color:                ; Current color for fading effect
  49.     .ds    0x01
  50.  
  51. .sframe:            ; Current frame of the sprite
  52.     .ds    0x01
  53.  
  54. .bposx:
  55.     .ds    0x02        ; Background position (fixed point)
  56. .bposy:
  57.     .ds    0x02
  58. .bspx:
  59.     .ds    0x02        ; Background speed (fixed point)
  60. .bspy:
  61.     .ds    0x02
  62.  
  63. .wposx:
  64.     .ds    0x02        ; Window position (fixed point)
  65. .wposy:
  66.     .ds    0x02
  67. .wspx:
  68.     .ds    0x02        ; Window speed (fixed point)
  69. .wspy:
  70.     .ds    0x02
  71.  
  72. .sposx:
  73.     .ds    0x02        ; Sprite position (fixed point)
  74. .sposy:
  75.     .ds    0x02
  76. .sspx:
  77.     .ds    0x02        ; Sprite speed (fixed point)
  78. .sspy:
  79.     .ds    0x02
  80.  
  81.     .area    _CODE
  82.  
  83.     .CLOSED        =    0x00
  84.     .OPENING    =    0x01
  85.     .OPENED        =    0x02
  86.     .CLOSING    =    0x03
  87.  
  88. _main::
  89.     DI            ; Disable interrupts
  90.     CALL    .wait_vbl    ; Must be in VBL before turning the screen off.
  91.  
  92.     LD    A,#0b01100111    ; LCD        = Off (no picture on screen)
  93.     LDH    (.LCDCONT),A
  94.                 ; WindowBank    =    0x9C00
  95.                 ; Window    =    On
  96.                 ; BG Chr    =    0x8800
  97.                 ; BG Bank    =    0x9800
  98.                 ; OBJ        =    8x16
  99.                 ; OBJ        =    On
  100.                 ; BG        =    On
  101.  
  102.     LD    A,#0b11100100
  103.     LDH    (.BGRDPAL),A
  104.     LDH    (.SPR0PAL),A
  105.  
  106.     XOR    A        ; A = 0
  107.     LD    (.time),A
  108.     LD    (.color),A
  109.  
  110.     ; Initialize tiles
  111.     LD    B,#0x00
  112.     CALL     .init_tiles    ; Init the tile set at 0x8000 with 0x00
  113.     LD    B,#0xFF
  114.     CALL     .init_btt    ; Init the tiles tables with 0xFF
  115.     CALL     .init_wtt
  116.  
  117.     LD    BC,#.tp0
  118.     LD    HL,#0x9000-(.endtp0-.tp0)
  119.     LD    DE,#.endtp0-.tp0
  120.     CALL    .mv_tiles    ; Move tiles
  121.  
  122.     LD    BC,#.tp1
  123.     LD    HL,#0x8000
  124.     LD    DE,#.endtp1-.tp1
  125.     CALL    .mv_tiles    ; Move tiles
  126.  
  127.     LD    BC,#.tp2
  128.     LD    HL,#0x8800
  129.     LD    DE,#.endtp2-.tp2
  130.     CALL    .mv_tiles    ; Move tiles
  131.  
  132.     LD    BC,#.tp3
  133.     LD    HL,#0x9000
  134.     LD    DE,#.endtp3-.tp3
  135.     CALL    .mv_tiles    ; Move tiles
  136.  
  137.     LD    BC,#.bkg_tiles
  138.     CALL    .set_btt
  139.  
  140.     ; Draw the frame in the window
  141.     LD    BC,#.frame_tiles
  142.     LD    DE,#0x0000/8    ; Place image at (0x00,0x00)
  143.     LD    HL,#0x8050/8    ; Image size is 0x80 x 0x50
  144.     CALL    .set_xy_wtt
  145.  
  146.     ; Draw the door in the window
  147.     LD    BC,#.door1_tiles
  148.     LD    DE,#0x1010/8    ; Place image at (0x10,0x10)
  149.     LD    HL,#0x6030/8    ; Image size is 0x60 x 0x30
  150.     CALL    .set_xy_wtt
  151.     LD    A,#.CLOSED
  152.     LD    (.doorstate),A
  153.  
  154.     ; Initialize background
  155.     XOR    A        ; A = 0
  156.     LD    (.bposx),A
  157.     LDH    (.SCROLLX),A
  158.     LD    (.bposx+1),A
  159.     LD    (.bposy),A
  160.     LDH    (.SCROLLY),A
  161.     LD    (.bposy+1),A
  162.     LD    A,#-0x01
  163.     LD    (.bspx),A
  164.     XOR    A        ; A = 0
  165.     LD    (.bspx+1),A
  166.     XOR    A        ; A = 0
  167.     LD    (.bspy),A
  168.     LD    A,#0x80
  169.     LD    (.bspy+1),A
  170.  
  171.     ; Initialize window
  172.     LD    A,#.MAXWNDPOSX
  173.     LD    (.wposx),A
  174.     LDH    (.WNDPOSX),A
  175.     XOR    A        ; A = 0
  176.     LD    (.wposx+1),A
  177.     LD    A,#.MAXWNDPOSY
  178.     LD    (.wposy),A
  179.     LDH    (.WNDPOSY),A
  180.     XOR    A        ; A = 0
  181.     LD    (.wposy+1),A
  182.     LD    A,#-0x01
  183.     LD    (.wspx),A
  184.     LD    A,#0x80
  185.     LD    (.wspx+1),A
  186.     LD    A,#-0x01
  187.     LD    (.wspy),A
  188.     LD    A,#0xC0
  189.     LD    (.wspy+1),A
  190.  
  191.     ; Initialize sprite
  192.     XOR    A        ; A = 0
  193.     LD    (.sframe),A
  194.     LD    C,#0x00        ; Sprite 0x00
  195.     LD    D,#0x00
  196.     CALL    .prop_sprite    ; Default sprite properties
  197.     LD    C,#0x01        ; Sprite 0x01
  198.     LD    D,#0x00
  199.     CALL    .prop_sprite    ; Default sprite properties
  200.  
  201.     LD    A,#0x10
  202.     LD    (.sposx),A
  203.     XOR    A        ; A = 0
  204.     LD    (.sposx+1),A
  205.     LD    A,#0x10
  206.     LD    (.sposy),A
  207.     XOR    A        ; A = 0
  208.     LD    (.sposy+1),A
  209.     XOR    A        ; A = 0
  210.     LD    (.sspx),A
  211.     LD    A,#0x40
  212.     LD    (.sspx+1),A
  213.     XOR    A        ; A = 0
  214.     LD    (.sspy),A
  215.     LD    A,#0x40
  216.     LD    (.sspy+1),A
  217.     CALL    .tile_sprite    ; Set sprite tiles
  218.     CALL    .place_sprite    ; Place sprites
  219.  
  220.     LD    A,#0b11100111    ; LCD Controller = On
  221.     LDH    (.LCDCONT),A
  222.     EI            ; Enable interrupts
  223. 1$:
  224.     LD    A,(.time)
  225.     INC    A
  226.     LD    (.time),A
  227.  
  228.     LD    BC,#.DELAY
  229.     CALL    .delay_ns
  230.  
  231.     CALL    .fade
  232.     CALL    .door
  233.     CALL    .scroll
  234.     CALL    .animate_sprite
  235.  
  236.     CALL    .jpad
  237.     LD    D,A
  238.  
  239.     AND    #.B        ; Is B pressed ?
  240.     JP    NZ,10$
  241.  
  242.     LD    A,D
  243.     AND    #.A        ; Is A pressed ?
  244.     JP    NZ,20$
  245.  
  246.     LD    A,D
  247.     AND    #.SELECT    ; Is SELECT pressed ?
  248.     JR    Z,2$
  249.     LD    A,#.STARTFADE
  250.     LD    (.color),A
  251. 2$:
  252.     LD    A,D
  253.     AND    #.START        ; Is START pressed ?
  254.     JR    Z,4$
  255.     LD    A,(.doorstate)
  256.     CP    #.CLOSED
  257.     JR    NZ,3$
  258.     LD    A,#.OPENING
  259.     LD    (.doorstate),A
  260.     XOR    A        ; A = 0
  261.     LD    (.doorpos),A
  262.     JR    4$
  263. 3$:
  264.     CP    #.OPENED
  265.     JR    NZ,4$
  266.     LD    A,#.CLOSING
  267.     LD    (.doorstate),A
  268.     LD    A,#.NBDFRAMES
  269.     LD    (.doorpos),A
  270. 4$:
  271.     LD    A,D
  272.     AND    #.UP        ; Is UP pressed ?
  273.     JR    Z,5$
  274.     LD    B,#0x00        ; Load 0x0010 into BC
  275.     LD    C,#0x10
  276.     LD    A,(.sspy)    ; Load speed into HL
  277.     LD    H,A
  278.     LD    A,(.sspy+1)
  279.     LD    L,A
  280.     LD    A,L        ; Substract BC from HL
  281.     SUB    C
  282.     LD    (.sspy+1),A
  283.     LD    A,H
  284.     SBC    B
  285.     LD    (.sspy),A    ; Store new speed
  286.     JR    6$
  287. 5$:
  288.     LD    A,D
  289.     AND    #.DOWN        ; Is DOWN pressed ?
  290.     JR    Z,6$
  291.     LD    B,#0x00        ; Load 0x0010 into BC
  292.     LD    C,#0x10
  293.     LD    A,(.sspy)    ; Load speed into HL
  294.     LD    H,A
  295.     LD    A,(.sspy+1)
  296.     LD    L,A
  297.      ADD    HL,BC        ; Add them
  298.     LD    A,H        ; Store new speed
  299.     LD    (.sspy),A
  300.     LD    A,L
  301.     LD    (.sspy+1),A
  302. 6$:
  303.     LD    A,D
  304.     AND    #.LEFT        ; Is LEFT pressed ?
  305.     JR    Z,7$
  306.     LD    B,#0x00        ; Load 0x0010 into BC
  307.     LD    C,#0x10
  308.     LD    A,(.sspx)    ; Load speed into HL
  309.     LD    H,A
  310.     LD    A,(.sspx+1)
  311.     LD    L,A
  312.     LD    A,L        ; Substract BC from HL
  313.     SUB    C
  314.     LD    (.sspx+1),A
  315.     LD    A,H
  316.     SBC    B
  317.     LD    (.sspx),A    ; Store new speed
  318.     JP    1$
  319. 7$:
  320.     LD    A,D
  321.     AND    #.RIGHT        ; Is RIGHT pressed ?
  322.     JP    Z,1$
  323.     LD    B,#0x00        ; Load 0x0010 into BC
  324.     LD    C,#0x10
  325.     LD    A,(.sspx)    ; Load speed into HL
  326.     LD    H,A
  327.     LD    A,(.sspx+1)
  328.     LD    L,A
  329.      ADD    HL,BC        ; Add them
  330.     LD    A,H        ; Store new speed
  331.     LD    (.sspx),A
  332.     LD    A,L
  333.     LD    (.sspx+1),A
  334.     JP    1$
  335.  
  336. 10$:
  337.     LD    A,D
  338.     AND    #.UP        ; Is UP pressed ?
  339.     JP    Z,11$
  340.     LD    B,#0x00        ; Load 0x0010 into BC
  341.     LD    C,#0x10
  342.     LD    A,(.bspy)    ; Load speed into HL
  343.     LD    H,A
  344.     LD    A,(.bspy+1)
  345.     LD    L,A
  346.     LD    A,L        ; Substract BC from HL
  347.     SUB    C
  348.     LD    (.bspy+1),A
  349.     LD    A,H
  350.     SBC    B
  351.     LD    (.bspy),A    ; Store new speed
  352.     JR    12$
  353. 11$:
  354.     LD    A,D
  355.     AND    #.DOWN        ; Is DOWN pressed ?
  356.     JP    Z,12$
  357.     LD    B,#0x00        ; Load 0x0010 into BC
  358.     LD    C,#0x10
  359.     LD    A,(.bspy)    ; Load speed into HL
  360.     LD    H,A
  361.     LD    A,(.bspy+1)
  362.     LD    L,A
  363.      ADD    HL,BC        ; Add them
  364.     LD    A,H        ; Store new speed
  365.     LD    (.bspy),A
  366.     LD    A,L
  367.     LD    (.bspy+1),A
  368. 12$:
  369.     LD    A,D
  370.     AND    #.LEFT        ; Is LEFT pressed ?
  371.     JP    Z,13$
  372.     LD    B,#0x00        ; Load 0x0010 into BC
  373.     LD    C,#0x10
  374.     LD    A,(.bspx)    ; Load speed into HL
  375.     LD    H,A
  376.     LD    A,(.bspx+1)
  377.     LD    L,A
  378.     LD    A,L        ; Substract BC from HL
  379.     SUB    C
  380.     LD    (.bspx+1),A
  381.     LD    A,H
  382.     SBC    B
  383.     LD    (.bspx),A    ; Store new speed
  384.     JP    1$
  385. 13$:
  386.     LD    A,D
  387.     AND    #.RIGHT        ; Is RIGHT pressed ?
  388.     JP    Z,1$
  389.     LD    B,#0x00        ; Load 0x0010 into BC
  390.     LD    C,#0x10
  391.     LD    A,(.bspx)    ; Load speed into HL
  392.     LD    H,A
  393.     LD    A,(.bspx+1)
  394.     LD    L,A
  395.      ADD    HL,BC        ; Add them
  396.     LD    A,H        ; Store new speed
  397.     LD    (.bspx),A
  398.     LD    A,L
  399.     LD    (.bspx+1),A
  400.     JP    1$
  401.  
  402. 20$:
  403.     LD    A,D
  404.     AND    #.UP        ; Is UP pressed ?
  405.     JP    Z,21$
  406.     LD    B,#0x00        ; Load 0x0010 into BC
  407.     LD    C,#0x10
  408.     LD    A,(.wspy)    ; Load speed into HL
  409.     LD    H,A
  410.     LD    A,(.wspy+1)
  411.     LD    L,A
  412.     LD    A,L        ; Substract BC from HL
  413.     SUB    C
  414.     LD    (.wspy+1),A
  415.     LD    A,H
  416.     SBC    B
  417.     LD    (.wspy),A    ; Store new speed
  418.     JR    22$
  419. 21$:
  420.     LD    A,D
  421.     AND    #.DOWN        ; Is DOWN pressed ?
  422.     JP    Z,22$
  423.     LD    B,#0x00        ; Load 0x0010 into BC
  424.     LD    C,#0x10
  425.     LD    A,(.wspy)    ; Load speed into HL
  426.     LD    H,A
  427.     LD    A,(.wspy+1)
  428.     LD    L,A
  429.      ADD    HL,BC        ; Add them
  430.     LD    A,H        ; Store new speed
  431.     LD    (.wspy),A
  432.     LD    A,L
  433.     LD    (.wspy+1),A
  434. 22$:
  435.     LD    A,D
  436.     AND    #.LEFT        ; Is LEFT pressed ?
  437.     JP    Z,23$
  438.     LD    B,#0x00        ; Load 0x0010 into BC
  439.     LD    C,#0x10
  440.     LD    A,(.wspx)    ; Load speed into HL
  441.     LD    H,A
  442.     LD    A,(.wspx+1)
  443.     LD    L,A
  444.     LD    A,L        ; Substract BC from HL
  445.     SUB    C
  446.     LD    (.wspx+1),A
  447.     LD    A,H
  448.     SBC    B
  449.     LD    (.w